package edu.nf.hotel.roompreddao.impl;

import edu.nf.hotel.roompred.RoomPred2;
import edu.nf.hotel.roompreddao.RoomPredDAO;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Data 2023-06-29
 * @Author Lsw
 */
public class RoomPredDaoImpl implements RoomPredDAO {
    @Override
    public List<RoomPred2> listManage(RoomPred2 roomPred2, int page, int limit) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select room_id,room_name,room_price,room_states,rp_id,predetermine_date,checkout_time,");
        sql.append("predatermine_time,numbers,u_name,room_card,telephone ");
        sql.append(" from room_info join roompred_info on room_id = r_id where room_states=1");


        if (roomPred2 != null && roomPred2.getUname() != null && !"".equals(roomPred2.getUname().trim())){
            sql.append(" and u_name like ?");
            params.add("%" + roomPred2.getUname() + "%");
        }
        sql.append(" limit ?,?");
        params.add(page);
        params.add(limit);
        BeanListHandler<RoomPred2> handler = new BeanListHandler<>(RoomPred2.class);
        return executor.executeQuery(sql.toString(),handler,params.toArray());
    }

    @Override
    public Long count(RoomPred2 roomPred2) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select count(*) from room_info join roompred_info on room_id=r_id");
        sql.append(" where room_states=1");
        if (roomPred2 != null && roomPred2.getUname() != null && !"".equals(roomPred2.getUname().trim())){
            sql.append(" and u_name like ?");
            params.add("%" + roomPred2.getUname() + "%");
        }
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql.toString(), handler, params.toArray());
    }
}
